package cn.syw.dao;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;

import cn.syw.dao.iface.PermissionDao;
import cn.syw.db.utils.JdbcTemplate;
import cn.syw.domain.Permission;
public class PermissionJdbcDaoImpl implements PermissionDao{
	private static Logger log = Logger.getLogger(PermissionJdbcDaoImpl.class);
	@Override
	public List<Permission> getPersission(String userName) {
		String sql = "select p.permission,p.url from users u left outer join userRole ur on u.id=ur.userId\r\n" + 
				"    left outer join permissionRole pr on ur.roleid=pr.roleId\r\n" + 
				"    left outer join permission p on pr.permissionId=p.id\r\n" + 
				"    where u.name = ? ";
		List<Permission> permissions = JdbcTemplate.query(sql, (rs)->{
			List<Permission> permissions1 = new ArrayList<>();
			try {
				while(rs.next()) {
					Permission permission = new Permission();
					permission.setPermission(rs.getString(1));
					permission.setUrl(rs.getString(2));
					permissions1.add(permission);
				}
			} catch (SQLException e) {
				log.debug("the result of url error:"+e.getMessage());
			}
			return permissions1;
		}, userName);
		return permissions;
	}
	@Override
	public List<Map<String, Object>> queryPermissions() {
		String sql = "select pr.id,r.rolename,p.permission,p.url " + 
				"from role r " + 
				"right outer join permissionrole pr on r.id = pr.roleid " + 
				"left outer join permission p on pr.permissionid = p.id ";
		List<Map<String, Object>> list = JdbcTemplate.query(sql, (rs)->{
			List<Map<String, Object>> list1 = new ArrayList<>();
			try {
				while(rs.next()) {
					Map<String,Object> map = new HashMap<>();
					for(int len = rs.getMetaData().getColumnCount(),i=1;i<=len;i++) {
						map.put(rs.getMetaData().getColumnName(i), rs.getObject(i));
					}
					list1.add(map);
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
			return  list1;
		}, new Object[] {});
		return list;
	}
	@Override
	public Map<String, Object> queryById(String id) {
		String sql = "select pr.id,r.rolename,p.permission,p.url " + 
				"from role r " + 
				"right outer join permissionrole pr on r.id = pr.roleid " + 
				"left outer join permission p on pr.permissionid = p.id " + 
				"where pr.id = ?";
		Map<String, Object> permission = JdbcTemplate.query(sql, (rs)->{
			Map<String,Object> per = new HashMap<>();
			try {
				while(rs.next()) {
					for(int len=rs.getMetaData().getColumnCount(),i=1;i<=len;i++) {
						per.put(rs.getMetaData().getColumnName(i), rs.getObject(i));
					}
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
			return per;
		}, id);
		return permission;
	}
	@Override
	public int deletePermission(String id) {
		String sql = "delete form permissionRole where id = ?";
		int num = JdbcTemplate.update(sql, id);
		return num;
	}

}
